/**
 * 二级物料BOM发布
 */
package ext.generic.integration.sourcing.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import wt.fc.WTObject;
import wt.part.WTPart;
import wt.part.WTPartUsageLink;
import wt.util.WTException;
import wt.vc.config.ConfigException;
import ext.com.core.CoreUtil;
import ext.generic.erp.common.CommonPDMUtil;
import ext.generic.integration.sourcing.task.TwoLevelMaterialBOMTask;
import ext.generic.integration.sourcing.task.TwoLevelMaterialTask;
import ext.tianma.part.MyPartUtil;
import ext.tianma.part.forms.TMTypeUtil;

public class TwoLevelMaterialBOMService {
	
	/**
	 * 发布二级料BOM
	 * @param wtObject
	 * @return boolean
	 */
	public static boolean releaseBOM( WTObject wtObject ){
		boolean flag = false;
		if( wtObject == null || !( wtObject instanceof WTPart ) ){
			return flag;
		}
		
		WTPart parentPart = ( WTPart )wtObject;
		
		if( TMTypeUtil.isRawMaterial( parentPart ) ){
			
			try {
				List< WTPartUsageLink > usageList = MyPartUtil.getFirstLevelUsageLink( parentPart );
				System.out.println( "INFO   usageList = "+ usageList.size() );
				if( usageList != null && usageList.size() > 0 ){
					//发布BOM主键
					//TwoLevelMaterialTask.add( parentPart );
					String parentView = parentPart.getViewName();
					//System.out.println( "Debug   parentPart="+parentPart.getDisplayIdentity() );
					flag = releasePartUsageLink( parentView , usageList );
				}
			} catch (ConfigException e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

	private static boolean releasePartUsageLink( String parentView , List< WTPartUsageLink > usageList ) throws WTException {
		
		boolean releaseFlag = false;
		//存放所有子件
		HashMap< WTPart , String > hashMap = new HashMap< WTPart , String >();
		
		Iterator< WTPartUsageLink > iterator = usageList.iterator();
		while( iterator.hasNext() ){
			WTPartUsageLink partUsageLink = iterator.next();
			
			String quantity = releasePartUsageLink( parentView , partUsageLink , hashMap );
			
			releaseFlag = TwoLevelMaterialBOMTask.add( partUsageLink , quantity ,"");
			if(!releaseFlag){
				break;
			}
		}
		return releaseFlag;
	}

	private static String releasePartUsageLink( String parentView , WTPartUsageLink partUsageLink , HashMap< WTPart , String > hashMap ) throws WTException {
		String counitQuantity = "";
		
		if( partUsageLink == null ){
			return counitQuantity;
		}
		
		WTPart seedPart = CoreUtil.getWTPartByMasterAndView( partUsageLink.getUses() , parentView );
		
		if( seedPart != null ){
			String usageQY = CommonPDMUtil.getQuantityValue( partUsageLink ) ;	
			
			if( usageQY != null && !usageQY.trim().isEmpty() ){
				BigDecimal usageBD = new BigDecimal( usageQY );
				
				if( hashMap.containsKey( seedPart ) ){
					String tableQY = hashMap.get( seedPart );
					
					if( tableQY != null && !tableQY.trim().isEmpty() ){
						BigDecimal tableBD = new BigDecimal(tableQY);
						
						counitQuantity = ( usageBD.add( tableBD ) ).toString();
						
						hashMap.remove( seedPart );
						hashMap.put( seedPart , counitQuantity );
					}	
				}else{
					hashMap.put( seedPart , usageQY );
					counitQuantity = usageQY ;
				}
			}

			//TwoLevelMaterialTask.add( seedPart );
		}
		
		return counitQuantity;
	}
}
